<?php

/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */
App::uses('Controller', 'Controller');

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package		app.Controller
 * @link		http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
 */
class AppController extends Controller {

    /**
     * Helpers
     *
     * @var array
     */
    public $helpers = array();

    /**
     * Components
     *
     * @var array
     */
    public $components = array(
        'Auth' => array(
            'loginAction' => array(
                'controller' => 'users',
                'action' => 'login',
                'plugin' => 'users'
            ),
            'logoutRedirect' => array(
                'controller' => 'users',
                'action' => 'login',
                'plugin' => 'users'
            ),
            'loginRedirect' => '/',
            'authorize' => array('Controller'),
            'authError' => 'Você realmente acha que você tem permissão para ver isso?',
            'authenticate' => array(
                'Form' => array(
                    'usuarioModel' => 'Usuarios.Usuario',
                    'fields' => array(
                        'usuarioname' => 'email',
                        'password' => 'password',
                    ),
                    'scope' => array('Usuario.status' => 1),
                    'recursive' => 1
                )
            )
        ),
        'Session',
        'Cookie',
        'Paginator',
        'Search.Prg'
    );

    /**
     * Preset vars
     *
     * @var array $presetVars
     * @link https://github.com/CakeDC/search
     */
    public $presetVars = true;

    /**
     * Constructor
     *
     * @param CakeRequest $request Request object for this controller. Can be null for testing,
     *  but expect that features that use the request parameters will not work.
     * @param CakeResponse $response Response object for this controller.
     */
    public function __construct($request = null, $response = null) {
        parent::__construct($request, $response);
        $this->helpers['Midia'] = array('className' => 'Midias.Midia');
        $this->helpers['Formatacao'] = array('className' => 'CakePtbr.Formatacao');
        $this->helpers['Less'] = array('className' => 'Midias.Less');
        $this->helpers['Html'] = array('className' => 'BoostCake.BoostCakeHtml');
        $this->helpers['Form'] = array('className' => 'BoostCake.BoostCakeForm');
        $this->helpers['Paginator'] = array('className' => 'BoostCake.BoostCakePaginator');
        $this->helpers['Session'] = array('className' => 'Session');
        $this->helpers['Time'] = array('className' => 'Time');
        $this->helpers['Text'] = array('className' => 'Text');
    }

    /**
     * Default isAuthorized method
     *
     * This is called to see if a usuario (when logged in) is able to access an action
     *
     * @param array $usuario
     * @return boolean True if allowed
     * @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#using-controllerauthorize
     */
    public function isAuthorized($usuario = null) {
        if (isset($this->request->params['prefix']) AND isset($usuario['role'])) {
            if ($this->request->params['prefix'] == $usuario['role']) {
                $this->Auth->allow();
                return TRUE;
            }
            return FALSE;
        }
        $this->Auth->allow();
        return TRUE;
    }

    public function beforeFilter() {
        if (isset($this->request->params['prefix']) AND $this->request->params['prefix'] == 'admin') {
            if (empty(AuthComponent::user())) {
                $this->redirect('/login');
            }
            $this->theme = 'default';
            $this->layout = 'admin';
        } else {
            $this->theme = 'BRA';
            $this->Auth->allow();
        }
        if ($this->request->is('ajax')) {
            $this->layout = 'ajax';
        }
        parent::beforeFilter();
    }

}
